#!/usr/bin/env bash
set -e

VERSION=${1:-1.8.0}

echo "Installing OpenJDK ${VERSION}..."

export JAVA_HOME=$(echo /tmp/lib/jvm/java-${VERSION}-openjdk-${VERSION}*)

if ! [ -d $JAVA_HOME ]; then
  set -x
  curl -sSL https://lambci.s3.amazonaws.com/binaries/java-${VERSION}-openjdk-devel.yumda.tgz | tar -xz -C /tmp
  export JAVA_HOME=$(echo /tmp/lib/jvm/java-${VERSION}-openjdk-${VERSION}*)
  set +x
fi
export _JAVA_OPTIONS="-Duser.home=$HOME"

# Also get Maven
if ! [ -d ~/apache-maven-3.6.3 ]; then
  set -x
  curl -sSL https://www-us.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz | tar -xz -C ~
  set +x
fi
export PATH=$HOME/apache-maven-3.6.3/bin:$PATH

echo "OpenJDK setup complete"
echo ""

set +e
